import { defineStore } from 'pinia'
import { RouteLocationNormalizedLoaded } from 'vue-router'


export const useTabsStore = defineStore({
  id: 'tabs',
  state: () => ({
    currentActive: '',
    tagList: [] as Array<RouteLocationNormalizedLoaded>,
  }),
  getters: {},
  actions: {
    addTags(view: RouteLocationNormalizedLoaded, index?: number) {
      if (this.tagList.some((v) => v.path === view.path)) return

      this.tagList.splice((index ?? this.tagList.length), 0, Object.assign({}, view))
    },

    deleteTags(path: string): number {
      for (const [i, v] of this.tagList.entries()) {
        if (v.path === path) {
          this.tagList.splice(i, 1)
          return i
        }
      }

      return 1
    },

    deleteAllTags() {
      for (let i = 0; i < this.tagList.length; i++) {
        if (this.tagList[i].name !== 'dashboard') {
          this.tagList.splice(i, 1)
          i--
        }
      }
    },
  },
})
